#!/usr/bin/perl
use strict;
use WebService::Simple;
use JSON qw/decode_json/;
use LWP::Simple;
binmode STDOUT, ":utf8";
print "Content-type: text/html \n\n";
my $service = WebService::Simple->new(
    base_url        => 'http://koebu.com/api/v2/hot_koes',
    response_parser => 'JSON'
);

my $res = $service->get( { per_page => '1', extended => '1'} );
my $json = decode_json( $res->content );
use Data::Dumper;
my $data = $json->{result}->[0];
#for my $key ( keys %{$data} ) {
	my $refresult = $json->{result};
	print "$data->{topic_image} <== topic_image\n";
	my $imageUrl = "http://koebu.com$data->{topic_image}";
	my $topicImage = get("$imageUrl");
	open(OUT,">sekinemoto.jpg");
	print OUT $topicImage;
	close(OUT);
	print "<h2>topic_image</h2><img src='./sekinemoto.jpg'/>"
#	open(OUT, "sekinemoto.jpg");
#	binmode IMG;
#  	binmode STDOUT;
#	print "Content-type: image/jpg\n\n";
##	print while (<OUT>);
#	close(OUT);
#}
